package com.example.springsecurity.service.Impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.springsecurity.Mapper.UserMapper;
import com.example.springsecurity.common.Result;
import com.example.springsecurity.common.ServiceException;
import com.example.springsecurity.entity.LoginUser;
import com.example.springsecurity.entity.User;
import com.example.springsecurity.service.UserService;
import com.example.springsecurity.utill.JwtUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    public AuthenticationManager authenticationManager;

    @Resource
    UserMapper userMapper;

    @Resource
    RedisTemplate<String, LoginUser> redisTemplate;

    @Override
    public Result<?> login(String name, String password) {
        //AuthenticationManager.authenticate进行用户认证
        UsernamePasswordAuthenticationToken authenticationToken=new UsernamePasswordAuthenticationToken(name, password);
        Authentication authentication= authenticationManager.authenticate(authenticationToken);
        //判断认证结果
        if(Objects.isNull(authentication)){
            throw new ServiceException("403","登录失败");
        }
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String id = String.valueOf(loginUser.getUser().getId());
        String token=JwtUtils.createJwt(id);
        redisTemplate.opsForValue().set("login:"+id, loginUser);
        redisTemplate.expire("login:"+id,600, TimeUnit.SECONDS);
        return Result.success("登陆成功",token);
    }

    @Override
    public Result<?> logout() {
        //获取SecurityContextHolder中的用户
        UsernamePasswordAuthenticationToken authenticationToken=(UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser=(LoginUser)authenticationToken.getPrincipal();
        String userid = loginUser.getUser().getId().toString();
        redisTemplate.delete("login:"+userid);
        return Result.success("注销成功");
    }

    @Override
    public Result<?> register(User user) {
        User u=userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getName,user.getName()));
        if(u!=null){
            throw new ServiceException("-1","用户名已存在");
        }
        userMapper.insert(user);
        //TODO
        //密码加密MD5
        return Result.success(user.getId());
    }

    @Override
    public Result<?> update(User user) {
        User u=userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getName,user.getName()));
        if(u==null){
            throw new ServiceException("-1","用户名不存在");
        }
        userMapper.update(user,Wrappers.<User>lambdaUpdate().eq(User::getId,user.getId()));
        return Result.success(user);
    }
}
